#!/bin/bash


prefix="/usr/local/mysql"
port=3306
while getopts "p:P:" arg
do
	case $arg in
	p)
	prefix=$OPTARG;
	;;
	P)
	port=$OPTARG;
	;;
	?)
	echo "无效的参数！"
	exit 1
	;;
	esac
done

echo $prefix
echo $port

# rpm if exist
rpm="gcc gcc-c++ ncurses-devel make cmake bison"
for i in $rpm
do
if  test -z `rpm -qa $i`
then
echo "$i wasn't installed!"
yum -y install $i
else
echo "$i was installed!"
fi
done 
echo "############check over#############"

groupadd mysql > /dev/null

useradd -g mysql mysql -s /sbin/nologin > /dev/null

tar -zxf Percona-Server-5.5.18-rel23.0.tar.gz

cd Percona-Server-5.5.18-rel23.0

cmake \
-DCMAKE_INSTALL_PREFIX:PATH=$prefix \
-DMYSQL_DATADIR:PATH=$prefix/data \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_UNIX_ADDR=$prefix/data/mysql.sock \
-DSYSCONFDIR=$prefix/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DMYSQL_USER=mysql \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_TCP_PORT=$port

make && make install

mkdir $prefix/etc

mkdir -p $prefix/logs/binlog
chown -R mysql:mysql $prefix/logs/

cp support-files/my-medium.cnf $prefix/etc/my.cnf

cd $prefix/scripts/

./mysql_install_db  --user=mysql  --basedir=$prefix --datadir=$prefix/data/

cp ../support-files/mysql.server /etc/init.d/mysql$port

chmod 755 /etc/init.d/mysql$port

/etc/init.d/mysql$port start

ps -ef | grep mysql

echo "安装完毕！！"
